import { ElMessage } from "element-plus";

export default class StrUtil {

    /**
     * 判断对象是否为空
     */
    static isNull(str:any) : boolean {
        if(str != null && str != 'null' && str != undefined && str != 'undefined' && str != '')
            return false
        return true
    }

    /**
     * 成功通知
     */
    static sr(content:string){
        ElMessage({ message: content, type: "success" });
    }
    
    /**
     * 失败通知
     */
    static er(content:string){
        ElMessage({ message: content, type: "error" });
    }

    /**
     * 复制对象
     */
    static cp(obj:Object){
        return JSON.parse(JSON.stringify(obj))

    }

    /**
     * 字符串列表是否有该字符串
     */
    static listHas(strList:Array<string>,str:string):boolean{
        for (let i = 0; i < strList.length; i++) {
            if(strList[i] == str)
                return true
        }
        return false
    }

    /**
     * 清除该字符串
     */
    static listRemove(strList:Array<string>,str:string):boolean{
        for (let i = 0; i < strList.length; i++) {
            if(strList[i] == str){
                strList.splice(i,1)
                return true
            }
        }
        return false
    }


    static findTree:any

    /**
     * 拿到子节点上一级对象
     @param fieldName 字段名
     @param fieldValue 内容
     @param tree 查找树
     @param childrenName 子节点字段
     */
    static getParent(fieldName:string,fieldValue:string,
        tree:any,childrenName:string) :any {
        this.findTree = {}
        this.findObj(fieldName,fieldValue,tree,childrenName)
        return this.findTree
    }

    private static findObj(fieldName:string,fieldValue:string,
        tree:any,childrenName:string) :any {
            if(tree[fieldName] == fieldValue)
                return tree
            else if(!StrUtil.isNull(tree[childrenName])){
                let arrRes = this.findChildren(fieldName,fieldValue,tree[childrenName],childrenName)
                if(arrRes!=null){
                    this.findTree = tree
                    return null
                }
            }

            else 
                return null
    }

    private static findChildren(fieldName:string,fieldValue:string,
        treeArr:any,childrenName:string) :any {
        for (let i = 0; i < treeArr.length; i++) {
            let res = this.findObj(fieldName,fieldValue,treeArr[i],childrenName)
            if(res!=null)
                return res
        }
        return null
    }

    static findFiledTree:any = []
    
    /**
     * 拿到节点上父级节点指定字段数组
     @param fieldName 字段名
     @param fieldValue 内容
     @param tree 查找树
     @param childrenName 子节点字段
     */
    static getParentFieldArr(fieldName:string,fieldValue:string,
        tree:any,childrenName:string) :any {
        this.findFiledTree = []
        this.findObjFieldArr(fieldName,fieldValue,tree,childrenName)
        return this.findFiledTree
    }
    private static findObjFieldArr(fieldName:string,fieldValue:string,
        tree:any,childrenName:string) :any {
            if(tree[fieldName] == fieldValue){
                this.findFiledTree.push(tree[fieldName])
                return tree
            }
            else if(!StrUtil.isNull(tree[childrenName])){
                let arrRes = this.findChildrenFieldArr(fieldName,fieldValue,tree[childrenName],childrenName)
                if(arrRes!=null){
                    this.findFiledTree.push(tree[fieldName])
                    return tree
                }
            }

            else 
                return null
    }

    private static findChildrenFieldArr(fieldName:string,fieldValue:string,
        treeArr:any,childrenName:string) :any {
        for (let i = 0; i < treeArr.length; i++) {
            let res = this.findObjFieldArr(fieldName,fieldValue,treeArr[i],childrenName)
            if(res!=null){
                return res
            }
        }
        return null
    }

}